package com.hl.sfzy.service.consultQAService.impl;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hl.sfzy.dto.ProblemDTO;
import com.hl.sfzy.entity.consutQA.Problem;
import com.hl.sfzy.entity.consutQA.Reply;
import com.hl.sfzy.mapper.consult.ConsultMapper;
import com.hl.sfzy.service.consultQAService.ConsultService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class ConsultServiceImpl implements ConsultService {

    private static final Logger log = LoggerFactory.getLogger(ConsultServiceImpl.class);
    @Autowired
    private ConsultMapper mapper;

    @Autowired
    private ObjectMapper objectMapper;


    @Override
    public ProblemDTO getProblemAndReply(Long problemId) {
        Problem problem = mapper.findProblemById(problemId);
        List<Reply> allComment = mapper.findAllComment(problemId);
        List<Reply> replies = flatReplay(allComment);
        ProblemDTO problemDTO = new ProblemDTO();
        problemDTO.setProblem(problem);
        problemDTO.setReplyList(replies);
        try{
            System.err.println(objectMapper.writeValueAsString(problemDTO));
        }catch (Exception e){
            e.printStackTrace();
        }
        return problemDTO;
    }


    public List<Reply> flatReplay(List<Reply> replies){
        final List<Reply> child = replies.stream().filter(reply -> reply.getParentReplyId() != null).collect(Collectors.toList());
        List<Reply> result = replies.stream().filter(reply -> reply.getParentReplyId() == null).collect(Collectors.toList());
        result.forEach(reply -> {
            List<Reply>  result2=new ArrayList<>();
            //递归
            reduceReply(child,result2,reply.getRId());
            reply.setReplyList(result2);
        });
        return  result;
    }

    //递归
    public  void  reduceReply(List<Reply> replies,List<Reply>  result2,long  pid){
        replies.stream().filter(reply -> reply.getParentReplyId()==pid).forEach(reply -> {
            result2.add(reply);
            reduceReply(replies,result2,reply.getRId());
        });
    }

    public  PageInfo<Problem> getAll(Integer pageNum,Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<Problem> byAll = mapper.findByAllProblems();
        PageInfo<Problem> pageInfo = new PageInfo<>(byAll);

        return pageInfo;
    }

    @Override
    public Problem getProblemById(Long id) {
        Problem problemById = mapper.findProblemById(id);
        return problemById;
    }







    public int addProblem(Problem problem){
        int i = mapper.addProblem(problem);

        return  i;
    }

    @Override
    public int deleteProblem(Long id) {
        int i = mapper.deleteProblem(id);

        return i;
    }

    @Override
    public int insertProblemComment(Reply reply) {
        int i = mapper.insertProblemComment(reply);

        return i;
    }
}
